using _ReplaceString_.UI.Styles;
using RSCore;
using Terraria.GameContent.UI.Elements;

namespace _ReplaceString_.UI.Elements;

public class ModElement : UIBase
{
	private const float IconSize = 40;

	private const float TextMargin = 6;

	private UIText _text;

	public readonly ModInfo Mod;

	public ModElement(ModInfo mod)
	{
		Mod = mod;
		ForegroundColor = Color.White;
		AddStyle(UISize.Fill, UISize.FixedHeight(50), UILayout.Horizontal);
		var iconPanel = new UIBase();
		iconPanel.AddStyle(UISize.Fill, UISize.FixedWidth(50));
		Append(iconPanel);
		float centralMargin = (Height.Pixels - IconSize) / 2;
		var icon = new ImageElement(mod.Icon)
		{
			Left = new(centralMargin, 0),
			Top = new(centralMargin, 0),
			Width = new(IconSize, 0),
			Height = new(IconSize, 0),
			Scale = IconSize / mod.Icon.Height,
		};
		iconPanel.Append(icon);

		_text = new UIText($"{mod.DisplayName} ({mod.Name})")
		{
			Height = new(0, 1),
			MarginLeft = TextMargin,
			TextOriginX = 0,
			TextOriginY = 0.5f,
			TextColor = ForegroundColor,
		};
		Append(_text);
		OnUpdate += ModElement_OnUpdate;
	}

	private void ModElement_OnUpdate(UIElement affectedElement)
	{
		_text.TextColor = ForegroundColor;
	}
}